
/**
 * 重算  联动计算 
 *
 *	先横向 计算 后纵向计算 
 *  
 */


function  docalPricedata(index,obj){

	//折扣率
	var detail_discount = document.getElementsByName("detail_discount");
	//var detail_discount = document.getElementById("detail_discount").value;
	//for(var i=0;i<detail_discount.length;i++){
		if(detail_discount[index-1].value == '' || detail_discount[index-1].value == 0.00)
		{
			alert("第"+index+"行的折扣率不能为0或空！");
			detail_discount[index-1].value=100.00;
			return false;
		}
	//}
	
	//计算行号
		editRow = 0;
		var rowId = document.getElementsByName("row_id");
		for(var i=0;i<rowId.length;i++){
			if(rowId[i].value == index){
				editRow = i;
				break;
			}
		}

		var innercode_all = document.getElementsByName("innercode");
		
	var is_taojian=false;
	if(innercode_all==null||innercode_all[editRow]==null){
	 
			docal_has_tax(editRow,obj.name,obj.value);
			doCalTotal_Amount();
			return ;
	}
		
		
//		is_taojian=docaliscompprod(innercode_alltemp[editRow].value,editRow,obj);


		var row=0;
		//主件的 行数
		var mainrow=0;
		 
		
		//数量
		var amountstemp = document.getElementsByName("amount");  
		
		var has_tax_price=document.getElementsByName("has_tax_price");
		
		var inner_length=innercode_all.length;
		
		var editInnercode=innercode_all[editRow].value;
		//主件的innercode
		var innercodetemp=editInnercode.substring(0,4);
		var amount_total=0;
		
		//折后金额（价税合计）
		var priceTaxSums = document.getElementsByName("discount_net_sum");
	
	
		var has_tax_price_total=0;
	
		if("discount_net_price"==obj.name){
			
				 
				docal_has_tax(editRow,'discount_net_price',obj.value);
				
			}
		
	 	if(editInnercode==null||""==editInnercode){
	 		mainrow=editRow;
	 	}else
	 	
		for(var i=0;i<inner_length;i++){
			if(innercode_all[i]!=null&&innercode_all[i].value!=""){
			
			
				var innercodeval=innercode_all[i].value;
			
				if(innercodeval==innercodetemp)
					mainrow=i;
				
				if(innercodeval.length>4){
					//所有子件的innercode
					var innercodevaltemp=innercodeval.substring(0,4);
					
					if(innercodevaltemp==innercodetemp){
						
						if(has_tax_price!=null&&has_tax_price[i]!=null&&""!=has_tax_price[i].value){
								 
									has_tax_price_total+=parseFloat(has_tax_price[i].value);
								 
							   
								}
								 
							 
								
						}
					}
				
				}
					
					
			}
	 
	
	 	//编辑的行和 主件是一行 就跳出
		if(editRow==mainrow) {
		
			docal_has_tax(editRow,obj.name,obj.value);
			doCalTotal_Amount();
			return;
		}
			
			
			
			
	
			//含税单价
			var hasTaxPrices = document.getElementsByName("has_tax_price");
		 
			
			hasTaxPrices[mainrow].value=has_tax_price_total;
			subReduNum(hasTaxPrices[mainrow],intPricePrecision);
	
			docal_has_tax(mainrow,'has_tax_price',has_tax_price_total);

			
			
			doCalTotal_Amount();
	 
//	
//		if(!is_taojian){
//			docal_has_tax(editRow,obj.name,obj.value);
//			doCalTotal_Amount();
//		}
		
	
}


/**
 * 净含税单价 联动套件的时候
 * 重新算下 含税单价
 * @return 
 * @type 
 */
function docal_has_tax(index,col_name,objvalue){

 
	if("discount_net_price"!=col_name&&"has_tax_price"!=col_name)
		return;
		
	//折扣率
	var discountRates = document.getElementsByName("detail_discount");
	 
	var discountrate=discountRates[index]==null||discountRates[index].value==null||""==discountRates[index].value?"100.0":discountRates[index].value;
	
	if("discount_net_price"==col_name){
			//含税单价
			var hasTaxPrices = document.getElementsByName("has_tax_price");
			
			//净含税单价 = 含税单价*折扣率
			
			var hastaxprice=(objvalue/ parseFloat(discountrate))*100;
			
			hasTaxPrices[index].value=hastaxprice;
			subReduNum(hasTaxPrices[index],intPricePrecision);
			
			
			
	}
	if("has_tax_price"==col_name){
			//净含税单价
			var discount_net_prices = document.getElementsByName("discount_net_price");
			
			//净含税单价 = 含税单价*折扣率
			
			var discount_net_price=(objvalue* parseFloat(discountrate))/100;
			
			discount_net_prices[index].value=discount_net_price;
			subReduNum(discount_net_prices[index],intPricePrecision);
	}
	setHas_tax_amount(index,col_name,objvalue);
	
}


//计算含税金额
function  setHas_tax_amount(i,col_name,objvalue){
		//数量
		var amountstemp = document.getElementsByName("amount");  
		//折后金额（价税合计）
		var priceTaxSums = document.getElementsByName("discount_net_sum");

		var amounts=amountstemp[i].value;
		var discount_net_sum=0;
			if(amounts>0){
			 	if(objvalue==null||objvalue=="")
			 		objvalue=0.0;
				discount_net_sum=parseFloat(objvalue)*parseFloat(amounts);
				//给含税金额赋值	
									
				priceTaxSums[i].value=discount_net_sum;
			}
									
			else{
				discount_net_sum=parseFloat(objvalue);
				//给含税金额赋值	
				priceTaxSums[i].value="0.0";
			}
			 
			subReduNum(priceTaxSums[i],intMnyPrecision);
			//doCalTotal_Amount();
}


/**
 * 	重新算一遍 代码有重复
 */

function doCalTotal_Amount(){


	//数量
	var amounts = document.getElementsByName("amount");  
	//折后价（含税净价或者无税竞价，统一取含税净价）
	var hasTaxNetPrices = document.getElementsByName("discount_net_price");
	

	
	//折后金额（价税合计）
	var priceTaxSums = document.getElementsByName("discount_net_sum");
	

	//更新报价单元合计数据
	var isProdItem = document.getElementsByName("is_prod_item");
	var totalAmount = document.getElementsByName("total_amount");
	var prodTotalAmount = document.getElementsByName("prod_total_amount");
	var lastTotalAmount = document.getElementsByName("last_total_amount");
	
	var innercode_all = document.getElementsByName("innercode");
	//是否子件
	var iscompprod_all = document.getElementsByName("iscompprod");
	var prodTotalAmountValue = 0;
	var lastTotalAmountValue = 0;
	
		
	for(var i=0;i<isProdItem.length;i++){
		if(isProdItem[i].value == "Y"){
			totalAmount[i].value = "0.00";
			for(var j=0;j<priceTaxSums.length;j++){
				//是否是套件
				var is_main=true;
				//如果是子件的话就不算总计金额了
				
					/*if(innercode_all!=null&&innercode_all[j]!=null){
						if(innercode_all[j].value!=null&&innercode_all[j].value.length>4)
								is_main=false;
					}*/
				if(iscompprod_all!=null && iscompprod_all[j]!=null){
					if(iscompprod_all[j].value!=null && iscompprod_all[j].value!="" && iscompprod_all[j].value == "Y")
						is_main=false;
				}
				if(!is_main)
					continue;
				 
				if(priceTaxSums[j].value==null||"null"==priceTaxSums[j].value||""==priceTaxSums[j].value){
					priceTaxSums[j].value="0.0";
					subReduNum(priceTaxSums[j],intMnyPrecision);
				}
					
				
				if(parseFloat(amounts[j].value) > 0){
					//数量不为空的情况，则累加金额
					if(priceTaxSums[j].value != "" && priceTaxSums[j].value != null && priceTaxSums[j].value != "null"){
						totalAmount[i].value = parseFloat(totalAmount[i].value) + parseFloat(priceTaxSums[j].value);
					} 
				}else{
					//数量为空时，则累加折后含税单价
					if(hasTaxNetPrices[j].value != "" && hasTaxNetPrices[j].value != null && hasTaxNetPrices[j].value != "null"){
						totalAmount[i].value = parseFloat(totalAmount[i].value) + parseFloat(hasTaxNetPrices[j].value);
					} 
				}
			}
			subReduNum(totalAmount[i],intMnyPrecision);
			prodTotalAmountValue = totalAmount[i].value;
		}
		if(totalAmount[i].value != "" && totalAmount[i].value != null && totalAmount[i].value != "null"){
			lastTotalAmountValue = lastTotalAmountValue + parseFloat(totalAmount[i].value);
		}
	}
		if(prodTotalAmount != null && prodTotalAmount[0] != undefined && prodTotalAmount[0] != null){
			prodTotalAmount[0].value = prodTotalAmountValue;
		}
		lastTotalAmount[0].value = lastTotalAmountValue;
		subReduNum(lastTotalAmount[0],intMnyPrecision);
}



function do_cal_discount_net_price(index){

		//计算行号
		editRow = 0;
		var rowId = document.getElementsByName("row_id");
		for(var i=0;i<rowId.length;i++){
			if(rowId[i].value == index){
				editRow = i;
				break;
			}
		}
		
	
		var discountRates = document.getElementsByName("detail_discount");
	 
		var discountrate=discountRates[editRow].value==null||""==discountRates[editRow].value?"100.0":discountRates[editRow].value;
	 		
			//净含税单价
			var discount_net_prices = document.getElementsByName("discount_net_price");
			
			var  discount_net_price=discount_net_prices[editRow].value;
			
	 		
			//含税单价
			var hasTaxPrices = document.getElementsByName("has_tax_price");
			
			//净含税单价 = 含税单价*折扣率
			
			var hastaxprice=(discount_net_price/ parseFloat(discountrate))*100;
			
			hasTaxPrices[editRow].value=hastaxprice;
			subReduNum(hasTaxPrices[editRow],intPricePrecision);
	

		   docalPricedata(index);

}


/**
 * 折后单价  =含税单价 * 折扣率
 * 
 *事件 加在 基价 和多价格项上
 *  
 */


function cal_dis_price(index){


	
		//计算行号
		editRow = 0;
		var rowId = document.getElementsByName("row_id");
		for(var i=0;i<rowId.length;i++){
			if(rowId[i].value == index){
				editRow = i;
				break;
			}
		}
		
		var discountRates = document.getElementsByName("detail_discount");
	 
		var discountrate=discountRates[editRow].value==null||""==discountRates[editRow].value?"100.0":discountRates[editRow].value;
	 	
			//净含税单价
			var discount_net_prices = document.getElementsByName("discount_net_price");
			
			//基价
			var base_price = document.getElementsByName("base_price");
			
			var base_price_total=0.0;
			var price_item_total=0.00;
			
			for(var i=1;i<=30;i++){
				
				var price_item=document.getElementsByName("price_item"+i);
				if(price_item==null||price_item=="undefined"||price_item==""||price_item.length<=0) 
					continue;
				
				if(	price_item[editRow]==null||	price_item[editRow]=="")
					continue;	
				
				var price_itemtemp=	price_item[editRow].value==null||price_item[editRow].value==""?0.0:price_item[editRow].value;
				
				price_item_total=parseFloat(price_item_total)+parseFloat(price_itemtemp);
				
			}
			
			if(base_price==null||base_price=="undefined"||base_price==""||base_price.length<=0)
				base_price_total=0;
			else
				base_price_total=base_price[editRow].value==null||base_price[editRow].value==""?0.0:base_price[editRow].value;
		 
			var total=parseFloat(base_price_total)+parseFloat(price_item_total);
			
			 
	 		
			//含税单价
			var hasTaxPrices = document.getElementsByName("has_tax_price");
			
			//净含税单价 = 含税单价*折扣率
			
			var discount_net_price=(parseFloat(total)* parseFloat(discountrate))/100;
			
			discount_net_prices[editRow].value=discount_net_price
			subReduNum(discount_net_prices[editRow],intPricePrecision);
	

		docal_has_tax(editRow,"discount_net_price",total)
		 doCalTotal_Amount();
	 	
	 		
	 	
}


